package com.ocom.company.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.company.EntityFlowConfig;
import com.ocom.common.entity.company.EntityFlowPushInfo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.request.company.SelectFlowPushInfoRequest;
import com.ocom.common.vo.FlowPushInfoVo;
import com.ocom.company.dao.IDaoFlowPushInfo;
import com.ocom.company.service.FlowPushInfoService;
import com.ocom.mysql.aspect.DataSourceAspect;
import groovy.util.logging.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class FlowPushInfoServiceImpl extends ServiceImpl<IDaoFlowPushInfo, EntityFlowPushInfo> implements FlowPushInfoService {
    @Override
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    public List<FlowPushInfoVo> selectFlowPushInfo(Long companyId, SelectFlowPushInfoRequest selectFlowPushInfoRequest) {
        QueryWrapper<EntityFlowPushInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(ObjectUtil.isNotNull(selectFlowPushInfoRequest.getFlowKeyword()),"c.flow_keyword",selectFlowPushInfoRequest.getFlowKeyword())
                    .eq("c.com_id",companyId)
                    .eq("p.line_id",selectFlowPushInfoRequest.getLineId());
        return this.baseMapper.selectFlowPushInfoList(queryWrapper);
    }

    @Override
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    public List<EntityFlowPushInfo> list(Long companyId, EntityFlowPushInfo entityFlowPushInfo) {
        return this.list(Wrappers.lambdaQuery(EntityFlowPushInfo.class)
                .eq(EntityFlowPushInfo::getComId,entityFlowPushInfo.getComId())
                .eq(EntityFlowPushInfo::getFId,entityFlowPushInfo.getFId())
        );
    }

    @Override
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    public boolean save(Long companyId, EntityFlowPushInfo entityFlowPushInfo) {
        return this.save(entityFlowPushInfo);
    }

    @Override
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    public boolean update(Long companyId, EntityFlowPushInfo entityFlowPushInfo) {
        return this.update(entityFlowPushInfo,Wrappers.lambdaQuery(EntityFlowPushInfo.class)
                .eq(EntityFlowPushInfo::getComId,entityFlowPushInfo.getComId())
                .eq(EntityFlowPushInfo::getId,entityFlowPushInfo.getId())
        );
    }

    @Override
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    public boolean remove(Long companyId, EntityFlowPushInfo entityFlowPushInfo) {
        if(ObjectUtil.isEmpty(entityFlowPushInfo.getFId()) && ObjectUtil.isEmpty(entityFlowPushInfo.getId())){
            log.error("删除审批人，参数FID或Id其中一个必须非空");
            throw new RuntimeException("系统错误");
        }
        return this.remove(Wrappers.lambdaQuery(EntityFlowPushInfo.class)
                .eq(EntityFlowPushInfo::getComId,entityFlowPushInfo.getComId())
                .eq(ObjectUtil.isNotEmpty(entityFlowPushInfo.getFId()),EntityFlowPushInfo::getFId,entityFlowPushInfo.getFId())
                .eq(ObjectUtil.isNotEmpty(entityFlowPushInfo.getId()),EntityFlowPushInfo::getId,entityFlowPushInfo.getId())
        );
    }


}
